<?php
// +----------------------------------------------------------------------
// | Wendasns [ 开源问答社区系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020~2021 http://wendasns.com All rights reserved.
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\wendasns\controller;

use app\wendasns\WenBaseController;

use app\wendasns\service\PwSeo;
use app\wendasns\model\Question;
use app\wendasns\model\Answer;
use app\wendasns\model\Article;
use app\wendasns\model\Fans;
use app\wendasns\model\Collect;
use app\wendasns\model\Notice;
use think\facade\View;
use wendasns\Wend;
use think\facade\Route;

class User extends WenBaseController
{
    public function run($dispatch='index')
    {
    	if(!$this->loginUser->isLogin) error('请登录后再操作',[],['refresh'=>0]);
    	if(!in_array($dispatch,['index','ask','answer','article','fans','collect','notice'])){
    		error('参数有误');
    	}
    	$this->$dispatch();
    	
    	$left = config('usermenu');
		$seo = new PwSeo();
		$seo->title = '用户中心';
		$seo->set();
    	View::assign(['left'=>$left,'action'=>$dispatch]);
        return View::fetch();
    }
    
    private function index()
    {
    	$sql = [
    		['answer','=',0],
    		['status','=','normal'],
    		['remove','=',0],
    		['close','=',0],
    		['user_id','<>',$this->loginUser->uid],
    	];
    	$dm = Question::where($sql)->order('create_time', 'desc')->paginate(10);//halt($dm->toArray());
    	/*$dm->withAttr('images',function($value, $data) {
        	return explode(',', $value);
        });
        foreach(){
        	
        }*/
    	View::assign('logs',$dm);
    }

    private function ask()
    {
    	$type = input('get.type','all');
    	$sql[] = ['user_id','=',$this->loginUser->uid];
    	switch ($type){
    		case 'adopt':
    			$sql[] = ['adopt_id','=',0];
    			break;
    		case 'answer':
    			$sql[] = ['answer','=',0];
    			break;
    		case 'solve':
    			$sql[] = ['answer','>',0];
    			break;
    		case 'delete':
    			$sql[] = ['remove','>',0];
    			break;
    		default:
    			$sql[] = ['id','>',0];
    	}
    	$dm = Question::where($sql)->order('create_time', 'desc')->paginate(10);//halt($dm->toArray());
    	View::assign(['type'=>$type,'logs'=>$dm]);
    }

    private function answer()
    {
    	$type = input('get.type','all');
    	$sql[] = ['user_id','=',$this->loginUser->uid];
    	switch ($type){
    		case 'adopt':
    			$sql[] = ['adopt','>',0];
    			break;
    			break;
    		case 'review':
    			$sql[] = ['status','=','check'];
    			break;
    		case 'delete':
    			$sql[] = ['remove','>',0];
    			break;
    		default:
    			$sql[] = ['id','>',0];
    	}
    	$dm = Answer::where($sql)->order('create_time', 'desc')->paginate(10);//halt($dm->toArray());
    	View::assign(['type'=>$type,'logs'=>$dm]);
    }

    private function article()
    {
    	$type = input('get.type','all');
    	$sql[] = ['user_id','=',$this->loginUser->uid];
    	switch ($type){
    		case 'review':
    			$sql[] = ['status','=','check'];
    			break;
    		case 'delete':
    			$sql[] = ['remove','>',0];
    			break;
    		default:
    			$sql[] = ['id','>',0];
    	}
    	$dm = Article::where($sql)->order('create_time', 'desc')->paginate(10);//halt($dm->toArray());
    	View::assign(['type'=>$type,'logs'=>$dm]);
    }
    
    private function notice()
    {
    	
    	$dm = Notice::where('user_id',$this->loginUser->uid)->order('create_time', 'desc')->paginate(10);//halt($dm->toArray());
    	View::assign('logs',$dm);
    }

    private function collect()
    {
    	$type = input('get.type','question');
    	$dm = Collect::where('user_id',$this->loginUser->uid)->where('mold',$type)->order('create_time', 'desc')->paginate(10);
    	View::assign(['type'=>$type,'logs'=>$dm]);
    }
    
    private function fans()
    {
    	$type = input('get.type','follower');//默认我的粉丝
    	if($type=='follower'){
    		$sql[] = ['target_id','=',$this->loginUser->uid];
    	}else{
    		$sql[] = ['user_id','=',$this->loginUser->uid];
    	}
    	$dm = Fans::where($sql)->order('create_time', 'desc')->paginate(10);
    	if($type=='follower'){
    		foreach($dm as $k=>$v){
    			$backs = $v->backs()->where('target_id',$v->user_id)->find();
    			$dm[$k]->follow = $backs;
    		}
    	}
    	View::assign(['type'=>$type,'logs'=>$dm]);
    }
}